package com.zw.weiyi.avtivity;

import android.content.ContentUris;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zw.weiyi.enety.Person;
import com.zw.weiyi.phonesms.BaseActivity;
import com.zw.weiyi.phonesms.R;
import com.zw.weiyi.util.MyApplication;

/**
 * 功能描述：弹出Activity界面
 * @author Striver_zw
 *   Date  2014-12-7
 */
public class DialogActivity extends BaseActivity implements OnClickListener {
	private LinearLayout layout01, layout02, layout03;
	private Person person;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_person_dialog);
		initView();
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		person = new Person();
		// 获取传过来的数据
		Intent intent = this.getIntent();
		person.setId(intent.getLongExtra("did", 0));
		person.setName(intent.getStringExtra("name"));
		// 得到布局组件对象并设置监听事件
		layout01 = (LinearLayout) findViewById(R.id.llayout01);
		layout02 = (LinearLayout) findViewById(R.id.llayout02);
		layout03 = (LinearLayout) findViewById(R.id.llayout03);

		layout01.setOnClickListener(this);
		layout02.setOnClickListener(this);
		layout03.setOnClickListener(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();// 点击其他地方就关闭该页面
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.llayout01:
			// 更新联系人
			if (person.getId() != null) {
				//把数据传递到编辑页面
				Intent intent = new Intent(DialogActivity.this,UpPerson.class);
				intent.putExtra("uid", person.getId());						
				startActivity(intent);				
				finish();
			}else {
				Toast.makeText(getApplicationContext(), "无法更新", 1000).show();
				finish();
			}
			break;
		case R.id.llayout02:
			// 删除联系人
			if (person.getId() != null) {
				getContentResolver().delete(//RawContacts.CONTENT_URI只能删除手机里面的
						ContentUris.withAppendedId(RawContacts.CONTENT_URI,
								person.getId()), null, null);
				Toast.makeText(getApplicationContext(), "删除成功", 1000).show();
			} else {
				Toast.makeText(getApplicationContext(), "删除失败", 1000).show();
			}
			finish();
			MyApplication.getInstance().finishActivity(InfoActivity.class);
			break;
		case R.id.llayout03:
			Intent intent = new Intent(DialogActivity.this,InfoSMSActivity.class);		
			intent.putExtra("number", person.getPhoneNumber());	
			intent.putExtra("name", person.getName());	
			startActivity(intent);		
			finish();
			break;
		}
	}
}
